--[[
   @classmod Screen
]]

local Camera = @@lib (camera)

return Class {
   init = function (self, w, h, scale)
      self.w, self.h = w, h
      self.x, self.y = 0, 0
      self.scale = scale or 4.5
      self.wide_scale = self.scale
      self.focus_scale = 55
      self:reload_camera ()
      self:center(self.w, self.h)
   end,
   
   is_hovered = function (self)
      local mx, my = Game.cursor:get_position ()
      return mx >= self.x and mx <= self.x + self.w - 8 and
	 my >= self.y and my <= self.y + self.h - 8
   end,
   
   reload_camera = function (self)
      self.camera = Camera.new(_lg.getWidth(), _lg.getHeight(), {
				  resizable = true, 
				  maintainAspectRatio = true,
				  translationX = 0, 
				  translationY = 0, 
				  scale = self.scale
      })
      self:center(self.w, self.h)
   end,
   
   center = function (self, w, h)
      self.camera:setTranslation ($LG.getWidth()/2-w/2, $LG.getHeight()/2-h/2)
   end,

   center_view = function (self, x, y)
      self.camera:setTranslation (x, y)
   end,
   
   resize = function (self, w, h)
      self:reload_camera()
   end,
   
   get_mouse_position = function (self)
      return self.camera:getMouseWorldCoordinates()
   end,
   
   update = function (self, dt)
      if self:is_hovered () then Game.cursor.show = true 
      else Game.cursor.show = nil
      end
      self.camera:update()
   end,
   
   draw  = function (self, tbl)
      if not tbl then return end
      self.camera:push()
      for k, v in ipairs(tbl) do
	 if v.draw then
	    v:draw()
	 else
	    if v.typeOf and v:typeOf("Drawable") then
	       _lg.draw(v)
	    end
	 end
      end
      self.camera:pop()
   end,
   
   to_local = function (self, x, y)
      return self.camera:getWorldCoordinates(x, y)
   end,
   
   to_screen = function (self, x, y)
      return self.camera:getScreenCoordinates(x, y)
   end,
   
   zoom_on_mouse_by = function (self, z)
      local zoom = z * self.camera:getScale() * 5
      self.camera:increaseScaleToPoint(zoom)
      self.scale = self.camera:getScale()
   end,
   
   
}
